#!/bin/python

#
#Xithyl, Test Driven Development for C
#Copyright (C) 2012  Stepan Henek
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

from xithyl import logger
from xithyl.Ctypes.basic import *
from xithyl.Ctypes import libc
from xithyl.conditions import basic
from xithyl import actions
from ctypes import *

logger.addLogger(__name__)

def functional1():
    """
    Create string.
    Use strchr to find the character in the string. (success)
    Check the result.
    """

    result = True

    logger.log("Functional 1", __name__)

    logger.log("Init string", __name__)

    str1 = makeArray(30, ord('q'))
    str1 = cast(str1, c_char_p)

    # Test assignment
    result &= basic.arrayEqualsLen([ord('q')] * 30, charArrayToList(str1, 30), 30, __name__, True)

    # Set character
    string = "string with a matching q is here"
    setStringInArray(str1, 0, string)

    # Test the setting
    result &= basic.arrayEqualsLen(map(lambda x: ord(x), string) + [0] + [ord('q')] * 19, charArrayToList(str1, 30), 30, __name__, True)

    # Call strchr
    res = libc.string.strchr(str1, ord('q'))

    # Test the string
    result &= basic.stringEqual(string[string.index('q'):], res, __name__, True)

    logger.log("Functional 1 done", __name__)

    return result

def functional2():
    """
    Create string.
    Use strchr to find the character in the string. (fail)
    Check the result.
    """

    result = True

    logger.log("Functional 2", __name__)

    logger.log("Init string", __name__)

    str1 = makeArray(30, ord('q'))
    str1 = cast(str1, c_char_p)

    # Test assignment
    result &= basic.arrayEqualsLen([ord('q')] * 30, charArrayToList(str1, 30), 30, __name__, True)

    # Set character
    string = "string with a matching X is here"
    setStringInArray(str1, 0, string)

    # Test the setting
    result &= basic.arrayEqualsLen(map(lambda x: ord(x), string) + [0] + [ord('q')] * 19, charArrayToList(str1, 30), 30, __name__, True)

    # Call strchr
    res = libc.string.strchr(str1, ord('q'))

    # Test the pointer
    result &= basic.numbersEquals(0, getReferencedAddress(res), __name__, True)

    logger.log("Functional 2 done", __name__)

    return result

def functional3():
    """
    Create string.
    Use strchr to find the character '\\0' in the string. (success)
    Check the result.
    """

    result = True

    logger.log("Functional 3", __name__)

    logger.log("Init string", __name__)

    str1 = makeArray(30, ord('\0'))
    str1 = cast(str1, c_char_p)

    # Test assignment
    result &= basic.arrayEqualsLen([ord('\0')] * 30, charArrayToList(str1, 30), 30, __name__, True)

    # Set character
    string = "string with a matching q is here"
    setStringInArray(str1, 0, string)

    # Test the setting
    result &= basic.arrayEqualsLen(map(lambda x: ord(x), string) + [0] + [ord('q')] * 19, charArrayToList(str1, 30), 30, __name__, True)

    # Call strchr
    res = libc.string.strchr(str1, ord('\0'))

    # Test the string
    result &= basic.stringEqual("", res, __name__, True)

    logger.log("Functional 3 done", __name__)

    return result
